//
//  CZQHomePageChildTwoVC.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/22.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQHomePageChildTwoVC.h"
#import "CZQHomeCourseCCell.h"
#import "CourseClassInfoViewController.h"
@interface CZQHomePageChildTwoVC ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    UICollectionViewFlowLayout *flowLayout;
}
@property(nonatomic,strong)UICollectionView * collectionView;
/**
 所有数据
 */
@property(nonatomic,strong)NSMutableArray *dataArrM;

@property(nonatomic,assign)CGFloat height;
@end

@implementation CZQHomePageChildTwoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"课程";
    
    [self collectionView];
}


#pragma  mark -懒加载
-(NSMutableArray *)dataArrM
{
    if (!_dataArrM) {
        _dataArrM = [NSMutableArray array];
    }

    return _dataArrM;

}


-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        flowLayout= [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//滚动方向
        flowLayout.minimumLineSpacing = 20;//行间距(最小值)
        flowLayout.minimumInteritemSpacing = 10;//item间距(最小值)
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);//设置section的边距
        
        
        CGFloat width = kScreenWidth - flowLayout.sectionInset.left - flowLayout.sectionInset.right;
        CGFloat w= floorf((width - (2 - 1) * flowLayout.minimumInteritemSpacing) / 2);
        
        
        _height = w/4*3 + 90;
        
        flowLayout.itemSize = CGSizeMake(w, _height);
        
        //item的大小
        //        flowLayout.headerReferenceSize = CGSizeMake(kScreenWidth, 20);
        //        flowLayout.footerReferenceSize = CGSizeMake(kScreenWidth, 20);
        //
        
        UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout ];
        //注册cell
        
        [collection registerNib:[UINib nibWithNibName:@"CZQHomeCourseCCell" bundle:nil] forCellWithReuseIdentifier:@"CZQHomeCourseCCell"];
        
        collection.scrollEnabled = NO;
        
        
        collection.backgroundColor = [UIColor whiteColor];
        //设置代理
        collection.delegate = self;
        collection.dataSource = self;
        [self.view addSubview:collection];
        
        [collection mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view);
            
        }];
        
        //        flowLayout.estimatedItemSize = CGSizeMake(w, w);
        
        _collectionView = collection;
        
    }
    return _collectionView;
}

#pragma mark ---- UICollectionViewDelegate && UICollectionViewDatasource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    
    
    return self.dataArrM.count + 4;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CZQHomeCourseCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CZQHomeCourseCCell" forIndexPath:indexPath];
    
    
    
    return cell;
}



//设置cell的大小


//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    __block NSInteger countItem;
//    __block CGFloat w;
//    dispatch_block_t block = ^{
//        NSLog(@"%f",flowLayout.sectionInset.left);
//        CGFloat width = self.frame.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right;
//        w= floorf((width - (countItem - 1) * flowLayout.minimumInteritemSpacing) / countItem);
//    };
//
//
//
//    countItem = 2;
//    block();
//
//
//    _height = w/4*3 + 90;
//
//
//    return CGSizeMake(w, w/4*3 + 90);
//
//}



//点击cell触发
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CourseClassInfoViewController *course = [CourseClassInfoViewController new];
    
    [self.navigationController pushViewController:course animated:YES];
    
}

//是否允许点击cell
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
